const http = require('../../utils/http')
const api = require('../../utils/api')
const ui = require('../../utils/ui')
const util = require('../../utils/util')

import Dialog from "../../components/dialog/dialog"

let appData = getApp().globalData
let pageSize = 5 //每页显示多少数据 

Page({

  refresh() {
    console.log('订单列表刷新')
    this.onShow()
  },

  dataInit() {
    this.data.dataList = [
      {
        currentPage: 1,
        loadMore: false,
        loadAll: false,
        list: [],
      },
      {
        currentPage: 1,
        loadMore: false,
        loadAll: false,
        list: [],
      },
      {
        currentPage: 1,
        loadMore: false,
        loadAll: false,
        list: [],
      },
      {
        currentPage: 1,
        loadMore: false,
        loadAll: false,
        list: [],
      },
    ]
  },

  getData() {
    let activeTab = this.data.activeTab
    let data = this.data.dataList[activeTab]
    let params = {
      memberId: getApp().globalData.memberId,
      pageNumber: data.currentPage,
      pageSize: pageSize,
    }
    if (activeTab == 1) {
      params["state"] = 2
    }
    else if (activeTab == 2) {
      params["state"] = 1
    }
    else if (activeTab == 3) {
      params["state"] = 0
    }
    //第一次加载数据
    if (data.currentPage == 2) {
      data.loadMore = true
      data.loadAll = false
      this.setData({
        dataList: this.data.dataList
      })
    }
    http.request({
      showLoading: true,
      url: api.orderList,
      data: params
    }).then(res=>{
      let records = res.data.result.records
      if (records.length > 0) {
        let list = data.list.concat(records)
        data.currentPage++
        data.list = list
        data.loadMore = false
        this.setData({
          dataList: this.data.dataList,
        })
        if (res.data.result.records.length < pageSize) {
          data.loadMore = false
          data.loadAll = true
          this.setData({
            dataList: this.data.dataList,
          })
        }
      }
      else {
        data.loadMore = false
        data.loadAll = true
        this.setData({
          dataList: this.data.dataList,
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  },

  onLoad() {
    
  },

  onShow() {
    this.dataInit()
    this.getData()
  },

  reachBottom() {
    let activeTab = this.data.activeTab
    let data = this.data.dataList[activeTab]
    if (!data.loadAll) {
      data.loadMore = true
      data.loadAll = false
      this.setData({
        dataList: this.data.dataList
      })
      setTimeout(() => {
        this.getData()
      }, 700)
    }
  },

  data:{
    scrollHeight: appData.screenHeight-(appData.tabBarHeight+appData.navBarHeight)-50,//50是头部tab高度，10是scroll向上的偏移量
    tabs: ['全部订单', '进行中订单', '待消费订单', '待支付订单'],
    activeTab: 0,
    dataList: null,
    wifi: "",
    password: "",
  },

  _updateSelectedPage(page) {
    let {activeTab} = this.data;
    activeTab = page;
    this.setData({activeTab: activeTab})
  },
  handlerTabTap(e) {
    this._updateSelectedPage(e.currentTarget.dataset.index);
  },
  swiperChange: function( e ) {
    this.setData( {
      activeTab: e.detail.current,
      navScrollLeft:e.detail.current >= 4 ? ((e.detail.current) * 60) : 0
    });
    let activeTab = this.data.activeTab
    let data = this.data.dataList[activeTab]
    if (data.list.length == 0) {
      this.getData()
    }
  },
  switchToOrder(){
    wx.switchTab({
      url: '/pages/store_detail/store_detail',
    })
  },
  showOrderDialog(event) {
    let oid = event.detail
    console.log('oid', oid)
    Dialog.confirm({
      selector: "#order-dialog",
      title: "提前消费提示",
      cancelButtonText: "取消",
      confirmButtonText: "确定",
      confirmCallback:() => {
        let comp = this.selectComponent("#id_"+oid)
        console.log('comp', comp)
        comp.gameStart()
      },
    });
  },

  showWifiDialog(event) {
    let ssid = event.detail.wifi
    let password = event.detail.password
    this.setData({
      wifi,
      password
    })
    Dialog.confirm({
      selector: "#wifi-dialog",
      title: "店铺WIFI",
      cancelButtonText: "取消",
      confirmButtonText: "一键连接",
      confirmCallback:() => {
        util.wifiConnect({
          ssid: ssid,
          password: password
        })
      },
      cancelCallback:() => {
        // wx.setClipboardData({
        //   data: this.data.storeInfo?.password ?? "",
        //   success: function (res) {
        //     ui.showToast({
        //       title: '复制成功'
        //     })
        //   }
        // })
      }
    });
  },
})